<template>
  <div>
    <el-button type="primary" @click="dialogVisible=true">新增角色</el-button>
    <el-table :data="list" border style="width: 100%">
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column prop="name" label="角色" width="180" />
      <el-table-column prop="description" label="描述" width="180" />
      <el-table-column label="操作">

        <template v-slot="{row}">
          <el-button type="success" size="medium" @click="assignPerm(row.id)">分配权限</el-button>
          <el-button type="primary" size="small" @click="edit(row.id)">编辑</el-button>
          <el-button type="danger" size="small" @click="del(row.id)">删除</el-button>
        </template>

      </el-table-column>
    </el-table>
    <!-- 分配权限 -->
    <el-dialog title="分配权限" :visible="showPermDialog">
      <!-- 防止属性组件和确认按钮 -->
      <el-tree
        ref="permTree"
        title="分配权限"
        :data="permData"
        :props="{label:'name'}"
        show-checkbox
        :default-checked-keys="selectCheck"
        check-strictly
        default-expand-all
        node-key="id"
      />
      <!-- 确定和取消按钮 -->
      <!-- 确定 取消 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- -----------------------------新增角色--------- -->
    <el-dialog
      :title="showTitle"
      :visible.sync="dialogVisible"
      width="60%"
      @close="btnCancel"
    >
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="角色名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="form.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分页 -->
    <el-row type="flex" justify="center" align="middle" style="height:60px">
      <el-pagination
        :current-page="page.page"
        :page-size="page.pagesize"
        :total="page.total"
        layout="prev, pager, next"
        @current-change="changePage"
      />
    </el-row>

  </div>
</template>

<script>

import { getRoleList, addRole, delRole, getDetail, editRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'

export default {
  data() {
    return {
      showPermDialog: false,
      selectCheck: [], // 用来记录当前角色所拥有的权限
      permData: [], // 专门用来接收权限数据 树形数据
      roleId: null, // 用来记录角色点的是哪个
      list: [], // 承接数组
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 5,
        total: 0 // 记录总数
      },
      dialogVisible: false,
      tableData: [{
        id: '',
        name: '',
        description: ''
      }],
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    /* changeSize(pagesize) {
      this.page.pagesize = pagesize
      this.getRoleList()
    }, */
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getRoleList()
    },
    async edit(id) {
      this.form.id = ''
      this.dialogVisible = true
      this.form = await getDetail(id)
    },
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    submit() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          if (this.form.id) {
            await editRole(this.form)
          } else {
            await addRole(this.form)
          }
          this.$message.success('操作成功')
          this.getRoleList()
          this.dialogVisible = false
        } else {
          this.$message.error('表单验证失败')
        }
      })
    },
    btnCancel() {
      this.form = {
        name: '',
        description: ''
      }
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    del(id) {
      this.$confirm('确定删除此角色?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRole(id)
        this.getRoleList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async btnPermOK() {
      // 应该实时获取当前选中的节点
      // 接口就是要选中的id节点数组
      await assignPerm({ id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    },
    // 取消
    btnPermCancel() {
      this.selectCheck = [] // 重置数据
      this.showPermDialog = false
    },
    // 分配权限
    async assignPerm(id) {
      // 要记录id  保存  需要知道存到哪个id上去
      // id用来查询当前角色到底有哪些权限
      this.roleId = id
      // 获取所有权限的数据
      // 需要将list数据转化成treeData数据
      this.permData = tranListToTreeData(await getPermissionList(), '0')
      const { permIds } = await getDetail(id)
      // permIds是复写的内容  √上权限点的复选框
      this.selectCheck = permIds // 记录当前用户权限
      // permIds是当前用户所有拥有的权限点数据
      this.showPermDialog = true // 显示弹层
    }
  }
}
</script>

<style scoped>
.el-table{
  margin-top: 10px;
}
.el-table-column{
  text-align: center;
}
</style>
